package com.jy.appmarket.view.dialog;


import com.jy.appmarket.R;

import android.app.Dialog;
import android.content.Context;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;


/**
 * @author xinhui.cheng
 *
 */
public class TextAndCheckBoxDialog extends Dialog{

	private RelativeLayout mTitleLayout;
	private TextView mTitle_tv;
	private ImageView mIcon_iv;
	private TextView mContent_tv;
	private Button mButton;
	private CheckBox mCheckBox;
	
	/*
	 * 图标类型
	 */
	public static final int TYPE_ERROR = 1;
	public static final int TYPE_OK = 2;
	public static final int TYPE_INFO = 3;
	public static final int TYPE_QUESTION = 4;
	
	public TextAndCheckBoxDialog(Context context) {
		super(context,R.style.custom_dialog_theme);
		init();
	}
	
	public void init(){
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.text_and_checkbox_dialog);
//		getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
		
		mTitleLayout = findViewById(R.id.text_dialog_title_layout);
		mTitle_tv = findViewById(R.id.text_dialog_title_tv);
		mIcon_iv = findViewById(R.id.text_dialog_icon_iv);
		mContent_tv = findViewById(R.id.text_dialog_content_tv);
		mButton = findViewById(R.id.dialog_button1);
		mCheckBox = findViewById(R.id.dialog_checkbox);
		
		//将默认隐藏的组件设为隐藏
		mTitleLayout.setVisibility(View.GONE);
		mTitle_tv.setVisibility(View.GONE);
		mIcon_iv.setVisibility(View.GONE);

	}
	
	/**
	 * 设置并显示标题
	 * @param title 要显示的标题字符串
	 * @return 当前的对话框对象
	 */
	public TextAndCheckBoxDialog showTitle(String title){
		if(title != null && !"".equals(title)){
			mTitle_tv.setText(title);
			mTitle_tv.setVisibility(View.VISIBLE);
			mTitleLayout.setVisibility(View.VISIBLE);
		}
		return this;
	}
	/**
	 * 设置并显示标题
	 * @param resId 要显示的标题的字符串资源ID
	 * @return 当前的对话框对象
	 */
	public TextAndCheckBoxDialog showTitle(int resId){
		mTitle_tv.setText(resId);
		mTitle_tv.setVisibility(View.VISIBLE);
		mTitleLayout.setVisibility(View.VISIBLE);
		return this;
	}
	/**
	 * 设置提示内容
	 * @param content 要提示的内容字符串
	 * @return 当前的对话框对象
	 */
	public TextAndCheckBoxDialog setContent(String content){
		if(content != null && !"".equals(content)){
			mContent_tv.setText(content);
		}
		return this;
	}
	/**
	 * 设置提示内容
	 * @param resId 要提示的字符串资源ID
	 * @return 当前的对话框对象
	 */
	public TextAndCheckBoxDialog setContent(int resId){
		mContent_tv.setText(resId);
		return this;
	}
	
	public TextAndCheckBoxDialog setContentGravity(int gravity){
		mContent_tv.setGravity(gravity);
		return this;
	}
	
	public TextAndCheckBoxDialog setLayoutPadding(int left,int top,int right,int bottom){
		RelativeLayout contentLayout = findViewById(R.id.text_dialog_content_layout);
		contentLayout.setPadding(left, top, right, bottom);
		return this;
	}
	
	/**
	 * 设置并显示自定义图标
	 * @param imageResId 图片资源ID
	 * @return 当前的对话框对象
	 */
	public TextAndCheckBoxDialog showCustomIcon(int imageResId){
		mIcon_iv.setImageResource(imageResId);
		mIcon_iv.setVisibility(View.VISIBLE);
		return this;
	}
	/**
	 * 设置并显示提示图标
	 * @param type 要显示的图标类型，可选值(TYPE_ERROR,TYPE_OK,TYPE_INFO,TYPE_QUESTION)
	 * @return 当前的对话框对象
	 */
	public TextAndCheckBoxDialog showHintIcon(int type){
		mIcon_iv.setVisibility(View.VISIBLE);
		switch(type){
/*			case TYPE_ERROR:
				mIcon_iv.setImageResource();
				break;
			case TYPE_OK:
				mIcon_iv.setImageResource(R.drawable.temp);
				break;
			case TYPE_INFO:
				mIcon_iv.setImageResource(R.drawable.temp);
				break;
			case TYPE_QUESTION:
				mIcon_iv.setImageResource(R.drawable.temp);
				break;*/
			default:
				mIcon_iv.setVisibility(View.GONE);
				break;
		}
		return this;
	}
	
	public TextAndCheckBoxDialog setButtonText(int resId){
		mButton.setText(resId);
		return this;
		
	}
	
	public TextAndCheckBoxDialog setButtonText(String text){
		mButton.setText(text);
		return this;
	}
	
	public TextAndCheckBoxDialog setOnClickListener(View.OnClickListener l){
		mButton.setOnClickListener(l);
		return this;
	}
	
	public TextAndCheckBoxDialog showCheckBox(int visibility){
		mCheckBox.setVisibility(visibility);
		return this;
	}
	
	public boolean getCheckBoxState(){
		return mCheckBox.isChecked();
	}
	
	@Override
	public boolean dispatchKeyEvent(android.view.KeyEvent event) {
		if(event.getKeyCode() == KeyEvent.KEYCODE_DPAD_CENTER
    			|| event.getKeyCode() == KeyEvent.KEYCODE_DPAD_DOWN
    			|| event.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP
    			|| event.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT
    			|| event.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT){
    		return true;
    	}
		
		return super.dispatchKeyEvent(event);
		
	}
}
